﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Gao.Poker.App.ViewModel;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using MessageBox = Xceed.Wpf.Toolkit.MessageBox;

namespace Gao.Poker.App.Game
{
    /// <summary>
    /// Interaction logic for GameMain.xaml
    /// </summary>
    public partial class GameMain : UserControl
    {
        private static GameMain _instance;
        private readonly GameViewModel _viewModel;
        private readonly GameView _gameView;
        private readonly GameEdit _gameEdit;
        private readonly ApplyView _applyView;
        private readonly EndContestView _endContestView;
        private readonly Dictionary<int, GameControl> _dictionary;


        public static GameMain Instance()
        {
            return _instance ?? (_instance = new GameMain());
        }

        public GameMain()
        {
            InitializeComponent();
            _viewModel = new GameViewModel();
            _viewModel.Message = OnMessage;
            DataContext = _viewModel;

            _gameEdit = new GameEdit();
            _gameEdit.OnAction = Save_Competition;
            _gameView= new GameView();
            _gameView.OnOut = OnOut;
            _applyView = new ApplyView();
            _applyView.ViewModel = _viewModel;
            _applyView.CallBack = CallBack;

            _endContestView = new EndContestView();
            _endContestView.ViewModel = _viewModel;
            _endContestView.CallBack = EndCallBack;

            cc.Content = _gameView;

            _dictionary = new Dictionary<int, GameControl>();

            btnSearch.Click += Search_Click;
        }

        private void EndCallBack(bool obj)
        {
            if (_dictionary.ContainsKey(_viewModel.Competition.ID))
            {
                var win = _dictionary[_viewModel.Competition.ID];
                if (win != null)
                {
                    win.ClosePalyGame();
                }
            }

            CallBack(true);

            _viewModel.OnPropertyChanged("Members");
            _viewModel.OnPropertyChanged("AllCompetitions");
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            _viewModel.PageIndex = 0;
        }


        private void OnOut(string obj)
        {
            var competition = dgCompetition.SelectedItem as Competition;

            if (_viewModel.Competition.State == 1)
            {
                OnMessage("比赛以结束");
                return;
            }

            if (competition != null)
            {
                var entry = CompetitionEntryService.Instance().GetMember(obj, competition.ID);
                var seating = SeatingService.Instance().GetMemberSeat(competition.ID, obj);
                entry.OutState = 1;
                CompetitionEntryService.Instance().Save(entry);
                if(seating != null)
                {
                    seating.MemberCardNo = String.Empty;
                    SeatingService.Instance().Save(seating);
                }
                OnMessage("出局成功");
                _viewModel.OnPropertyChanged("Members");
            }
        }

        private void CallBack(bool obj)
        {
            if(obj)
            {
                cc.Content = _gameView;
            }
        }

        private void OnMessage(string msg)
        {
            MessageBox.Show(msg, "提示信息",MessageBoxButton.OK,MessageBoxImage.Information);
        }


        private void New_Click(object sender, RoutedEventArgs e)
        {
            dgCompetition.SelectedItem = null;
            cc.Content = _gameEdit;
            _viewModel.Competition = new Competition();
        }

        private void Modifiy_Click(object sender, RoutedEventArgs e)
        {
            if (dgCompetition.SelectedItem == null)
            {
                OnMessage("请选择要修改的比赛");
                return;
            }
            cc.Content = _gameEdit;
            _viewModel.Competition = dgCompetition.SelectedItem as Competition;
        }

        public void Save_Competition(List<Tables> list)
        {
            if (list.Count == 0)
            {
                OnMessage("请选择桌号");
                return;
            }

            CompetitionService service = new CompetitionService();
            _viewModel.Competition.TablesNum = String.Join(",", list.Select(a => a.Number).ToArray());
            TablesService ts = new TablesService();
            ts.Update(_viewModel.Competition.ID);
            var competition = service.Save(_viewModel.Competition, list);
            _viewModel.Competition = competition;

            _viewModel.OnPropertyChanged("AllCompetitions");
            OnMessage("保存成功");
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cc.Content = _gameView;
        }

        /// <summary>
        /// 报名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Apply_Click(object sender, RoutedEventArgs e)
        {

            if (dgCompetition.SelectedItem == null)
            {
                OnMessage("请选择要报名的比赛");
                return;
            }

            if(_viewModel.Competition.State == 1)
            {
                OnMessage("比赛以结束");
                return;
            }

            cc.Content = _applyView;
        }
        /// <summary>
        /// 结束比赛
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void End_Click(object sender, RoutedEventArgs e)
        {
            if (dgCompetition.SelectedItem == null)
            {
                OnMessage("请选择要结束的比赛");
                return;
            }

            if (_viewModel.Competition.State == 1)
            {
                OnMessage("比赛以结束");
                return;
            }

            _viewModel.UpdateRanking();
            cc.Content = _endContestView;
        }

        /// <summary>
        /// 买入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BuyStake_Click(object sender, RoutedEventArgs e)
        {
            if (dgCompetition.SelectedItem == null)
            {
                OnMessage("请选择进行中的比赛");
                return;
            }

            if (_viewModel.Competition.State == 1)
            {
                OnMessage("比赛以结束");
                return;
            }

            BuyStakeWin win = new BuyStakeWin();
            win.ViewModel = _viewModel;
            win.Show();
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            if (dgCompetition.SelectedItem == null)
            {
                OnMessage("请选择进行中的比赛");
                return;
            }

            if (_viewModel.Competition.State == 1)
            {
                OnMessage("比赛以结束");
                return;
            }

            if(!_dictionary.ContainsKey(_viewModel.Competition.ID))
            {
                var control = new GameControl();
                control.CompetitionID = _viewModel.Competition.ID;
                _dictionary.Add(_viewModel.Competition.ID,control);
            }
            cc.Content = _dictionary[_viewModel.Competition.ID];
        }

        private void Pager_PageIndexChanging(int pageindex, EventArgs e)
        {
            _viewModel.PageIndex = pageindex;
        }
    }
}
